<?php
class Admin_ContactController extends Apps_Controller_Action {
	public function indexAction() {
	    //  Action Delete
		$this->actResolve('act_delete',
				'delete', null, null, array('id' => $this->_getParam('item_checker')),
				true);

		$tbContact = new M_DbTable_Contact();
		$grid = new Apps_Plugin_Grid('ContactGrid');
		// Set Grid
		$grid->setData($tbContact->getList())
			 ->setTableCaption('List Contact')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '1%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Customer',
			 			  'width'		=> '10%',
			 			  'dataField'	=> 'full_name'),
			 		array('title'		=> 'Email',
			 			  'html'        => array('<a href="mailto:%s" target="_blank">%s</a>',
			 			  				  array('email', 'email')),),
	 			    array('title'		=> 'Title',
	 			  	 	  'html'        => array('<input type="text" class="input-v" value="%s">',
	 			  						   array('subject')),),
 			  	    array('title'		=> 'Content',
 			  		      'html'        => function($rs) {
 			  									$l = 100;
 			  									return "<input type='text' class='input-v' value='". ((strlen($rs['message']) > $l)? substr($rs['message'], 0, $l).'...' : $rs['message']) ."' >";
	 			    		}),
	 			    array('title'		=> 'Status',
	 			           'html'	    => function ($rs) {
                    	 			           return ($rs['status'] == M_DbTable_Contact::STATUS_ACTIVE)?
                    	 			           '<a href="##" class="ajax-link-must-confirm popup hint  label label-success" data-ajax-to="/admin/contact/change-value?p=status&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="Checked">New</a>' :
                    	 			           '<a href="##" class="ajax-link-must-confirm popup hint  label label-warning" data-ajax-to="/admin/contact/change-value?p=status&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="New">Checked</a>';
                    	 			    },
	 			    'class'		=> 'center'),
	 			    array('title'		=> 'View',
	 			    		'html'        => array('<a href="/admin/contact/detail?id=%s"><i class="icon-list-alt"></i></a>',
	 			    				array('id')),
	 			    		'class'		=> 'center'),
			 		array('title'      => 'Delete',
			 			  'html'       => array('<a href="/admin/contact/delete?id=%s"><i class="icon-remove must-confirm " title="Xóa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%')
			 		));
		$grid->applySearch(new Admin_Form_Search_Contact());

		$this->view->grid = $grid;
	}

	public function detailAction() {
		$rq = $this->getRequest();
		$contactId = intval($rq->getParam('id'));
		$tbContact = new M_DbTable_Contact();

		$contact = $tbContact->getContactyById($contactId)->query()->fetch();

		$this->view->contact = $contact;
	}


	public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$contactIds = $rq->getParam('id');
		$tbContact = new M_DbTable_Contact();

		if(is_numeric($contactIds)) {
			$this->_flashMessenger->addMessage(
						$tbContact->safelyDelete(array('id = ?' => $contactIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
			$this->goToActionIndex();
		} elseif (is_array($contactIds)) {
			foreach ($contactIds as $contactId) {
				$tbContact->safelyDelete(array('id = ?' => $contactId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			$this->goToActionIndex();
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}
	}

	public function changeValueAction() {
	    $this->disableLayout(true, true);
	    $rq = $this->_request;
	    $param  = $rq->getParam('p');
	    $id 	= intval($rq->getParam('id'));
	    $tbContact = new M_DbTable_Contact();
	    if(!($r = $tbContact->fetchRow(array('id = ?' => $id))->toArray())) {
	        echo '<script>window.location=window.location</script>
	        <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	        . $this->getBacktrackUrl()
	        . '">Update Error (Record not found), please wait ...</a>';
	    }

	    switch ($param) {
	        case 'status':
	            ($r['status'] == M_DbTable_Contact::STATUS_ACTIVE)
	            ? $tbContact->safelyUpdate(array('status'  => M_DbTable_Contact::STATUS_INACTIVE), //unchecked
	            array('id =?'	  => $id))
	            : $tbContact->safelyUpdate(array('status'  => M_DbTable_Contact::STATUS_ACTIVE), //checked
	            array('id =?'  => $id));
	            break;
	        default: break;
	    }

	    echo '<script>location.href = location.href</script>
	    <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	    . $this->getBacktrackUrl()
	    . '">Update successfully, please wait ...</a>';
	}
}